package com.seo.controller.user;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.ReturnBody;
import com.seo.resolver.DefiniteException;
import com.seo.service.user.role.sdk.UserRoleService;
import com.seo.service.user.sdk.ConfigService;
import com.seo.service.user.sdk.UserChildService;
import com.seo.service.user.sdk.UserDomainService;
import com.seo.service.user.sdk.UserService;
import com.seo.utils.ClientConst;
import com.seo.utils.Const;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * maincontroller <br/>
 * 
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserDomainService userDomainService;

	@Autowired
	private UserChildService userChildService;
	
	@Autowired
	private UserRoleService userRoleService;
	
	@Autowired
	private ConfigService configService;

	@RequestMapping
	public String index(HttpServletRequest request, HttpSession session) {
		log.info("进入客户详情...");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		Map<String, Object> user = userService.getUserById(userId);

		request.setAttribute("user", user);

		return "user/userInfo";

	}

	@RequestMapping("/save")
	@ResponseBody
	public ReturnBody save(@RequestParam Map<String, Object> params, HttpSession session) {
		log.info("保存用户信息...");
		ReturnBody rb = ReturnBody.init();
		String id = (String) params.get("id");
		if (!StringUtils.isNumeric(id)) {
			log.error("用户ID不是数字");
			return rb.error("用户不存在");
		}

		Integer curUserId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		if (Integer.parseInt(id) != curUserId) {
			log.error("只能修改个人信息");
			return rb.error("用户不存在");
		}

		return userService.updateUser(params);
	}

	@RequestMapping("/toUpdatePwd")
	public String toUpdatePwd() {
		return "user/updatePwd";
	}

	@RequestMapping("/updatePwd")
	@ResponseBody
	public ReturnBody updatePwd(@RequestParam Map<String, Object> params, HttpSession session) {
		log.info("修改用户密码...");

		Integer id = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		params.put("id", id);

		return userService.updateUserPwd(params);
	}

	@RequestMapping("/price")
	public String price(Integer userId, HttpServletRequest request, HttpSession session) {
		log.info("编辑代理价...");

		Map<String, Object> user = userService.getUserById(userId);
		request.setAttribute("user", user);
		return "user/proxyUser/price";

	}

	@RequestMapping("/setPrice")
	@ResponseBody
	public ReturnBody setPrice(@RequestParam Map<String, Object> params) {
		log.info("保存代理价...");
		params.put("type", "00");
		return userService.updateProxyPrice(params);
	}

	@RequestMapping("/child/list")
	public String childList(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpSession session) {
		log.info("进入下级用户列表...");
		
		if (!Jurisdiction.roleCode("proxy")) {
			log.error("未开通代理功能，请联系管理员");
			throw new DefiniteException("未开通代理功能，请联系管理员");
		}
		
		ReturnBody body = userChildService.getChildUserList(params);
		if (!body.isSuccess()) {
			throw new DefiniteException(body.getResult().getMessage());
		}
		
		String seoDomain = userDomainService.getSeoDomain();
		request.setAttribute("page", body.get("page"));
		request.setAttribute("seoDomain", seoDomain);

		return "user/child/list";

	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public ReturnBody deleteUser(Integer id, HttpSession session) {
		log.info("删除用户...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		if (!Jurisdiction.roleCode("proxy")) {
			log.error("未开通代理功能，请联系管理员");
			throw new DefiniteException("未开通代理功能，请联系管理员");
		}
		
		Map<String, Object> user = (Map<String, Object>) session.getAttribute(Const.SESSION_USER);
		Integer userId = (Integer) user.get("id");
		if (userId == id) {
			return rb.error("error", "不能删除自己");
		}

		return userService.deleteUserFalse(id, "01");
	}
	
	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		log.info("进入添加用户页面");
		if (!Jurisdiction.roleCode("proxy")) {
			log.error("未开通代理功能，请联系管理员");
			throw new DefiniteException("未开通代理功能，请联系管理员");
		}
		List<Map<String, Object>> roleList = userRoleService.getAllRoleList();
		
		List<String> levelList = null;
		String clevel = configService.getConfig("vip_level");
		if(clevel != null) {
			String[] level = clevel.split(",");
			levelList = Arrays.asList(level);
		}
		request.setAttribute("levelList", levelList);
		request.setAttribute("roleList", roleList);
		request.setAttribute("action", "addUser");
		return "user/child/edit";
	}

	@RequestMapping("/addUser")
	public String addUser(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		if (!Jurisdiction.roleCode("proxy")) {
			log.error("未开通代理功能，请联系管理员");
			throw new DefiniteException("未开通代理功能，请联系管理员");
		}
		ReturnBody rb = ReturnBody.init();
		Integer parentId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		params.put("parentId", parentId);
		rb = userService.addUser(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}

	@RequestMapping("/edit")
	public String edit(Integer userId, HttpServletRequest request) {
		log.info("进入编辑用户页面");
		if (!Jurisdiction.roleCode("proxy")) {
			log.error("未开通代理功能，请联系管理员");
			throw new DefiniteException("未开通代理功能，请联系管理员");
		}
		List<Map<String, Object>> roleList = userRoleService.getAllRoleList();
		Map<String, Object> user = userService.getUserById(userId);
		
		List<String> levelList = null;
		String clevel = configService.getConfig("vip_level");
		if(clevel != null) {
			String[] level = clevel.split(",");
			levelList = Arrays.asList(level);
		}
		request.setAttribute("levelList", levelList);
		request.setAttribute("roleList", roleList);
		request.setAttribute("user", user);
		request.setAttribute("action", "editUser");
		return "user/child/edit";
	}
	
	@RequestMapping("/editUser")
	public String editUser(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		if (!Jurisdiction.roleCode("proxy")) {
			log.error("未开通代理功能，请联系管理员");
			throw new DefiniteException("未开通代理功能，请联系管理员");
		}
		ReturnBody rb = ReturnBody.init();
		rb = userService.updateUser(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}
	
	@RequestMapping("/updateTemplet")
	@ResponseBody
	public ReturnBody updateTemplet(String templet) {
		log.info("更换皮肤...");
		
		Integer id = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);

		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}
		
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		params.put("templet", templet);

		return userService.updateTemplet(params);
	}

	
	@RequestMapping("/toResetPwd")
	public String toResetPwd(Integer userId, HttpServletRequest request) {
		log.info("进入用户重置密码页面");
		if (!Jurisdiction.roleCode("proxy")) {
			log.error("未开通代理功能，请联系管理员");
			throw new DefiniteException("未开通代理功能，请联系管理员");
		}
		request.setAttribute("userId", userId);

		return "user/child/resetPwd";
	}
	
	@RequestMapping("/resetPwd")
	@ResponseBody
	public ReturnBody resetPwd(Integer id, String password, HttpSession session) {
		log.info("更改用户密码...");
		if (!Jurisdiction.roleCode("proxy")) {
			log.error("未开通代理功能，请联系管理员");
			throw new DefiniteException("未开通代理功能，请联系管理员");
		}
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}
		
		
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		params.put("password", password);

		return userService.updateResetUserPwd(params);
	}
	
	@RequestMapping("/proxyPrices")
	public String proxyPrices(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("初始化代理价格...");
		/*if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}*/
		if(params == null || params.isEmpty()) {
			throw new DefiniteException("参数错误");
		}
		String userId = params.get("userId")+"";
		Map<String, Object> user = userService.getUserById(Integer.parseInt(userId));
		int num = userService.getUserPriceToVipLevelNum(Integer.parseInt(userId));
		if(num > 0) {
			request.setAttribute("show","NO");
		} else {
			request.setAttribute("show","YES");
		}
		request.setAttribute("user", user);
		request.setAttribute("param", params);
		return "user/proxyUser/proxyPrice";

	}
	
	@RequestMapping("/setProxyPrice")
	@ResponseBody
	public ReturnBody setProxyPrice(@RequestParam Map<String, Object> params) {
		log.info("初始化代理价...");
		ReturnBody rb = ReturnBody.init();
		if (params == null || params.isEmpty()) {
			return rb.error("error", "参数不能为空");
		}
		return userService.saveProxyPrice(params);
	}
	
}
